package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.Restrictions;
import com.kuanter.kuanterauto.entity.WearherInfo;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.JsonParser;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;

@Deprecated
public class NewTQActivity extends BaseActivity_NoBar {
	Button left_back;
	TextView top_middle;
	LinearLayout home_chose_City;
	
	TextView current_tqqk;	//当前天气信息
	TextView curren_wd;		//当前天气温度
	
	ImageView today_img;  //当前天气图片
	TextView wea_xc;  //洗车指数
	TextView today_low;//当前温度
	TextView today_high;  //
	
	ImageView tomo_img; //明天天气图片
	TextView tomo_low;  //明天温度
	TextView tomo_high; 
	
	ImageView after_img;  //后天天气图片
	TextView after_tomo_low; //后天温度
	TextView after_tomo_high;
	
	LinearLayout cur_xxxx; //限行信息
	TextView cu_xxwh;  //信息尾号
	
	LinearLayout wea;
	
	public static String cityID;
	static Restrictions restriction;  //限行信息
	static WearherInfo wea_info = null;
	
	LayoutInflater factory;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_tq);
		factory = getLayoutInflater();
		initUI();
		
		initView();
		/*
		if(restriction == null || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			//获取限行信息
			DataLoader loader = new DataLoader(NewTQActivity.this);
			List<NameValuePair> params1 = new ArrayList<NameValuePair>();
			NameValuePair cityID = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(NewTQActivity.this));
			params1.add(cityID);
			//TODO
			loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.findRestriction, params1, restri_handler, "", false);
		} else {
			if(restriction.getdRRegion() != null && !restriction.getdRRegion().equals("")) {
				cur_xxxx.setVisibility(View.VISIBLE);
			} else {
				cur_xxxx.setVisibility(View.GONE);
			}
			if(restriction.getdRNumber() != null && !restriction.getdRNumber().equals("")) {
				cu_xxwh.setVisibility(View.VISIBLE);
				cu_xxwh.setText("今日限行尾号    " + restriction.getdRNumber().replace("[", "").replace("]", "").replaceAll("," , "、"));
			} else {
				cu_xxwh.setVisibility(View.GONE);
			}
		}
		
		if(wea_info == null  || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			String cityforweatherID = SharePrefenceUtil.getCityIDForWeather(NewTQActivity.this);
			DataLoader loadWeather = new DataLoader(this);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair nvp = new BasicNameValuePair("cityId",cityforweatherID);
			params.add(nvp);
			loadWeather.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDCITYWEATHER, params, wea_handler, "", false);
		}*/
		
		
	}

	View xxxx;  //限行信息;
	TextView rule;
	TextView region;
	TextView res_time;
	Dialog dialog;  //限行消息
	private void initView() {
		wea = (LinearLayout) findViewById(R.id.wea);
		
		current_tqqk = (TextView) findViewById(R.id.current_tqqk);
		curren_wd = (TextView) findViewById(R.id.curren_wd);
		
		today_img = (ImageView) findViewById(R.id.today_img);
		wea_xc = (TextView) findViewById(R.id.wea_xc);
		today_low = (TextView) findViewById(R.id.today_low);
		today_low.setTextColor(Color.parseColor("#E6FFBE"));
		today_high = (TextView) findViewById(R.id.today_high);
		today_high.setTextColor(Color.parseColor("#FFCD5A"));
		
		tomo_img = (ImageView) findViewById(R.id.tomo_img);
		tomo_low = (TextView) findViewById(R.id.tomo_low);
		tomo_low.setTextColor(Color.parseColor("#E6FFBE"));
		tomo_high = (TextView) findViewById(R.id.tomo_high);
		tomo_high.setTextColor(Color.parseColor("#FFCD5A"));
		
		after_img = (ImageView) findViewById(R.id.after_img);
		after_tomo_low = (TextView) findViewById(R.id.after_tomo_low);
		after_tomo_low.setTextColor(Color.parseColor("#E6FFBE"));
		after_tomo_high = (TextView) findViewById(R.id.after_tomo_high);
		after_tomo_high.setTextColor(Color.parseColor("#FFCD5A"));
		
		cur_xxxx = (LinearLayout) findViewById(R.id.cur_xxxx);
		cu_xxwh = (TextView) findViewById(R.id.cu_xxwh);
		
		cur_xxxx.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				xxxx = factory.inflate(R.layout.restriction, null);
				rule = (TextView) xxxx.findViewById(R.id.rule);
				region = (TextView) xxxx.findViewById(R.id.region);
				res_time = (TextView) xxxx.findViewById(R.id.res_time);
				dialog = new Dialog(NewTQActivity.this,R.style.selectorDialog);
				dialog.setTitle("限行信息");
				dialog.setContentView(xxxx);
				if(restriction == null) {
					showProgress("正在获取限行信息...");
					DataLoader loader = new DataLoader(NewTQActivity.this);
					List<NameValuePair> params1 = new ArrayList<NameValuePair>();
					NameValuePair cityID = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(NewTQActivity.this));
					params1.add(cityID);
					//TODO
					loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.findRestriction, params1, restri_handler, "", false);
				} else {
					rule.setText(restriction.getdRRule() + "");
					region.setText(restriction.getdRRegion() + "");
					dialog.show();
				}
				
			}
		});
		
		if(wea_info != null) {
			curren_wd.setText(wea_info.getCurrentTemp() + "°");
			
			String str_today = wea_info.getTemp().split(",")[0];
			int to_p1 = str_today.indexOf("℃");
			int to_p3 = str_today.indexOf("~");
			int to_p2 = str_today.lastIndexOf("℃");
			if(to_p1 >= 0) {
				String to_high = str_today.substring(0, to_p1);
				today_high.setText(to_high + "°");
			}
			if(to_p3 >= 0 && to_p2 >= 0 && to_p2 > to_p3) {
				String to_low = str_today.substring(to_p3 + 1, to_p2);
				today_low.setText(to_low + "°");
			}
			
			getImg(wea_info.getTq_info().split(",")[0], wea);
			
			current_tqqk.setText(wea_info.getTq_info().split(",")[0]);
			getImg(wea_info.getTq_info().split(",")[0], today_img);
			
			String str_tomorrow = wea_info.getTemp().split(",")[1];
			int tomo_p1 = str_tomorrow.indexOf("℃");
			int tomo_p3 = str_tomorrow.indexOf("~");
			int tomo_p2 = str_tomorrow.lastIndexOf("℃");
			if(tomo_p1 >= 0) {
				String tomorrow_high = str_tomorrow.substring(0, tomo_p1);
				tomo_high.setText(tomorrow_high + "°");
			}
			if(tomo_p3 >= 0 && tomo_p2 >= 0 && tomo_p2 > tomo_p3) {
				String tomorrow_low = str_tomorrow.substring(tomo_p3 + 1, tomo_p2);
				tomo_low.setText(tomorrow_low + "°");
			}
			getImg(wea_info.getTq_info().split(",")[1], tomo_img);
			
			String str_after = wea_info.getTemp().split(",")[2];
			int after_p1 = str_after.indexOf("℃");
			int after_p3 = str_after.indexOf("~");
			int after_p2 = str_after.lastIndexOf("℃");
			if(after_p1 >= 0) {
				String after_high = str_tomorrow.substring(0, after_p1);
				after_tomo_high.setText(after_high + "°");
			}
			if(after_p3 >= 0 && after_p2 >= 0 && after_p2 > after_p3) {
				String after_low = str_tomorrow.substring(after_p3 + 1, after_p2);
				after_tomo_low.setText(after_low + "°");
			}
			getImg(wea_info.getTq_info().split(",")[2], after_img);
			
			wea_xc.setText(wea_info.getXc_number() + "洗车");
			if(wea_info.getXc_number().equals(WearherInfo.bsy) || wea_info.getXc_number().equals(WearherInfo.jbsy)) {
				wea_xc.setTextColor(Color.WHITE);
			} else {
				wea_xc.setTextColor(Color.parseColor("#DEFF80")); 
			}
		}
	}

	private void initUI() {
		left_back = (Button) findViewById(R.id.left_back);
		left_back.setVisibility(View.GONE);
		
		top_middle = (TextView) findViewById(R.id.top_middle);
		top_middle.setText("出行信息");
		
		home_chose_City = (LinearLayout) findViewById(R.id.home_chose_City);
		home_chose_City.setVisibility(View.GONE);
		
	}
	
	
	/*
	 * 获取限行信息的hanlder
	 */
	Handler restri_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			finishProgress();
			if(msg.what == HttpTask.RESULT_OK) {
				String json = (String) msg.obj;
				Log.i("bbb", json + "限行");
				restriction = JsonParser.getRestri(json);
				if(restriction != null) {
					if(restriction.getdRRegion() != null && !restriction.getdRRegion().equals("")) {
						cur_xxxx.setVisibility(View.VISIBLE);
					} else {
						cur_xxxx.setVisibility(View.GONE);
					}
					
					if(restriction.getdRNumber() != null && !restriction.getdRNumber().equals("")) {
						
						cu_xxwh.setVisibility(View.VISIBLE);
						cu_xxwh.setText("今日限行尾号    " + restriction.getdRNumber().replace("[", "").replace("]", "").replaceAll("," , "、"));
						
					} else {
						
						cu_xxwh.setVisibility(View.GONE);
						
					}
				} else {
					cur_xxxx.setVisibility(View.GONE);
				}
			} else {
				showToast("网络不好，获取失败");
			}
			super.handleMessage(msg);
		}
		
	};
	
	
	Handler wea_handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == HttpTask.RESULT_OK) {
				//TODO
				cityID = SharePrefenceUtil.getCityID(NewTQActivity.this);
				
				String json = (String) msg.obj;
				wea_info = JsonParser.getWeatherInfo(json);
				if (wea_info != null) {
					
					// ---
					if (wea_info.getXc_number() != null) {
						wea_xc.setText(wea_info.getXc_number() + "洗车");
						if (wea_info.getXc_number().equals(WearherInfo.bsy)
								|| wea_info.getXc_number().equals(
										WearherInfo.jbsy)) {
							wea_xc.setTextColor(Color.WHITE);
						} else {
							wea_xc.setTextColor(Color.parseColor("#DEFF80"));
						}
					}
					// ----
//					middle_weather_road.setBackgroundResource(wea_info
//							.getImgRes());
					
					getImg(wea_info.getTq_info().split(",")[0], wea);
					
					getImg(wea_info.getTq_info().split(",")[0],today_img);
					curren_wd.setText(wea_info.getCurrentTemp() + "°");
					
					String str_today = wea_info.getTemp().split(",")[0];
					int to_p1 = str_today.indexOf("℃");
					int to_p3 = str_today.indexOf("~");
					int to_p2 = str_today.lastIndexOf("℃");
					if(to_p1 >= 0) {
						String to_high = str_today.substring(0, to_p1);
						today_high.setText(to_high + "°");
					}
					if(to_p3 >= 0 && to_p2 >= 0 && to_p2 > to_p3) {
						String to_low = str_today.substring(to_p3 + 1, to_p2);
						today_low.setText(to_low + "°");
					}
					current_tqqk.setText(wea_info.getTq_info().split(",")[0]);
					
					
					String str_tomorrow = wea_info.getTemp().split(",")[1];
					int tomo_p1 = str_tomorrow.indexOf("℃");
					int tomo_p3 = str_tomorrow.indexOf("~");
					int tomo_p2 = str_tomorrow.lastIndexOf("℃");
					if(tomo_p1 >= 0) {
						String tomorrow_high = str_tomorrow.substring(0, tomo_p1);
						tomo_high.setText(tomorrow_high + "°");
					}
					if(tomo_p3 >= 0 && tomo_p2 >= 0 && tomo_p2 > tomo_p3) {
						String tomorrow_low = str_tomorrow.substring(tomo_p3 + 1, tomo_p2);
						tomo_low.setText(tomorrow_low + "°");
					}
//					weather_tq_1.setText(wea_info.getTq_info().split(",")[1]);
					getImg(wea_info.getTq_info().split(",")[1], tomo_img);
					
					
					String str_after = wea_info.getTemp().split(",")[2];
					int after_p1 = str_after.indexOf("℃");
					int after_p3 = str_after.indexOf("~");
					int after_p2 = str_after.lastIndexOf("℃");
					if(after_p1 >= 0) {
						String after_high = str_tomorrow.substring(0, after_p1);
						after_tomo_high.setText(after_high + "°");
					}
					if(after_p3 >= 0 && after_p2 >= 0 && after_p2 > after_p3) {
						String after_low = str_tomorrow.substring(after_p3 + 1, after_p2);
						after_tomo_low.setText(after_low + "°");
					}
					
//					weather_tq_2.setText(wea_info.getTq_info().split(",")[2]);
//					getImg(wea_info.getTq_info().split(",")[2],middle_weather_road2);
					getImg(wea_info.getTq_info().split(",")[2], after_img);
				}
			}
			super.handleMessage(msg);
		}

	};
	
	@Override
	protected void onResume() {
		if(restriction == null || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			//获取限行信息
			DataLoader loader = new DataLoader(NewTQActivity.this);
			List<NameValuePair> params1 = new ArrayList<NameValuePair>();
			NameValuePair cityID = new BasicNameValuePair("cityCode",SharePrefenceUtil.getCityID(NewTQActivity.this));
			params1.add(cityID);
			//TODO
			loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.findRestriction, params1, restri_handler, "", false);
		} else {
			if(restriction.getdRRegion() != null && !restriction.getdRRegion().equals("")) {
				cur_xxxx.setVisibility(View.VISIBLE);
			} else {
				cur_xxxx.setVisibility(View.GONE);
			}
			if(restriction.getdRNumber() != null && !restriction.getdRNumber().equals("")) {
				
				cu_xxwh.setVisibility(View.VISIBLE);
				cu_xxwh.setText("今日限行尾号    " + restriction.getdRNumber().replace("[", "").replace("]", "").replaceAll("," , "、"));
				
			} else {
				
				cu_xxwh.setVisibility(View.GONE);
			}
		}
		
		
		
		if(wea_info == null  || cityID.equals("") || !cityID.equals(SharePrefenceUtil.getCityID(this))) {
			String cityforweatherID = SharePrefenceUtil.getCityIDForWeather(NewTQActivity.this);
			DataLoader loadWeather = new DataLoader(this);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair nvp = new BasicNameValuePair("cityId",cityforweatherID);
			params.add(nvp);
			loadWeather.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDCITYWEATHER, params, wea_handler, "", false);
		}
		
		super.onResume();
	}
	
	public void getImg(String s,ImageView iv) {
		//设置图片
		int position = s.lastIndexOf("转");
		if(position != -1) {
			s = s.substring(0, position);
		}
		if(s.contains("沙")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("霾")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("雷")) {
			iv.setImageResource(R.drawable.storm);
		} else if(s.contains("雾")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("晴")) {
			iv.setImageResource(R.drawable.sun);
		} else if(s.contains("云")) {
			iv.setImageResource(R.drawable.clouds);
		} else if(s.contains("阴")) {
			iv.setImageResource(R.drawable.fog_day);
		} else if(s.contains("雪")) {
			iv.setImageResource(R.drawable.snow);
		} else if(s.contains("雨")) {
			iv.setImageResource(R.drawable.rain);
		}
	}
	
	public void getImg(String s,LinearLayout iv) {
		//设置图片
		int position = s.lastIndexOf("转");
		if(position != -1) {
			s = s.substring(0, position);
		}
		if(s.contains("沙")) {
			iv.setBackgroundResource(R.drawable.wea_li);
		} else if(s.contains("霾")) {
			iv.setBackgroundResource(R.drawable.wea_li);
		} else if(s.contains("雷")) {
			iv.setBackgroundResource(R.drawable.wea_lei);
		} else if(s.contains("雾")) {
			iv.setBackgroundResource(R.drawable.wea_wu);
		} else if(s.contains("晴")) {
			iv.setBackgroundResource(R.drawable.wea_qtian);
		} else if(s.contains("云")) {
			iv.setBackgroundResource(R.drawable.wea_duoyun);
		} else if(s.contains("阴")) {
			iv.setBackgroundResource(R.drawable.wea_yin);
		} else if(s.contains("雪")) {
			iv.setBackgroundResource(R.drawable.wea_xue);
		} else if(s.contains("雨")) {
			iv.setBackgroundResource(R.drawable.wea_yu);
		}
	}
	
}
